/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.controller;

import com.anwen.mongo.toolkit.StringUtils;
import com.comac.ins.common.core.domain.R;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.common.web.core.BaseController;
import com.comac.ins.isfd.domain.bo.DataRetrievalExectSearchBo;
import com.comac.ins.isfd.domain.bo.DataRetrievalHistoryBo;
import com.comac.ins.isfd.domain.bo.DataRetrievalViewBo;
import com.comac.ins.isfd.domain.bo.ModelDataDeleteBo;
import com.comac.ins.isfd.domain.vo.*;
import com.comac.ins.isfd.service.IsfdDataRetrievalService;
import lombok.RequiredArgsConstructor;
import org.bson.Document;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 统一检索
 *
 * @date: 2025/2/21 16:00
 **/
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/isfd/dataRetrieval")
public class IsfdDataRetrievalController extends BaseController {

    private final IsfdDataRetrievalService isfdDataRetrievalService;


    /**
     * 检索首页数据统计
     * @return
     */
    @PostMapping("/count")
    public R<Map<String,Long>> dataRetrievalCount(){
        Long resultCount = isfdDataRetrievalService.dataRetrievalCount();
        Map result = new HashMap<>();
        result.put("count",resultCount);
        return R.ok(result);
    }

    /**
     * 条件检索(模糊匹配)
     * @param dataRetrievalHistoryBo
     * @return
     */
    @PostMapping("/search/range")
    public R<?> dataRetrievalSearchRange(@RequestBody DataRetrievalHistoryBo dataRetrievalHistoryBo){
        if(StringUtils.isNotBlank(dataRetrievalHistoryBo.getKeyWord())){
            IsfdDataRetrievalDataListVo resultObject = isfdDataRetrievalService.dataRetrievalSearchWithKeyword(dataRetrievalHistoryBo);
            return R.ok(resultObject);
        }else{
            IsfdDataRetrievalListVo resultObject = isfdDataRetrievalService.dataRetrievalSearch(dataRetrievalHistoryBo);
            return R.ok(resultObject);
        }
    }

    /**
     * 条件检索(精确匹配)
     * @param dataRetrievalExectSearchBo
     * @return
     */
    @PostMapping("/search/exact")
    public R<List<IsfdDatabaseModelConfigVo>> dataRetrievalSearchExact(@RequestBody DataRetrievalExectSearchBo dataRetrievalExectSearchBo){
        List<IsfdDatabaseModelConfigVo> resultObject = isfdDataRetrievalService.dataRetrievalSearchExact(dataRetrievalExectSearchBo);
        return R.ok(resultObject);
    }

    /**
     * 检索历史删除
     * @param id
     * @return
     */
    @DeleteMapping("/history/delete/{id}")
    public R<Void> dataRetrievalDelete(@PathVariable Long id){
       return  toAjax(isfdDataRetrievalService.dataRetrievalDeleteById(id));
    }

    /**
     * 检索历史结果查询
     * @return
     */
    @PostMapping("/history/list")
    public R<List<DataRetrievalHistoryVo>> dataRetrievalHistory(){
        List<DataRetrievalHistoryVo> resultList = isfdDataRetrievalService.dataRetrievalHistory();
        return R.ok(resultList);
    }

    /**
     * 检索历史结果保存
     * @param dataRetrievalHistoryBo
     * @return
     */
    @PostMapping("/history/save")
    public R<Void> dataRetrievalHisSave(@RequestBody DataRetrievalHistoryBo dataRetrievalHistoryBo){
        return  toAjax(isfdDataRetrievalService.dataRetrievalHisSave(dataRetrievalHistoryBo));
    }

    /**
     * 检索结果-数据查看
     * @param dataRetrievalViewBo
     * @return
     */
    @PostMapping("/view")
    public R<Map<?,?>> dataRetrievalView(@RequestBody DataRetrievalViewBo dataRetrievalViewBo){
        return R.ok(isfdDataRetrievalService.dataRetrievalView(dataRetrievalViewBo));
    }

    /**
     * 检索结果-数据导出
     * @return
     */
    @PostMapping("/export")
    public String dataRetrievalExport(){
        return "export";
    }
    /**
     * 模型数据删除
     */
    @PostMapping("/delete/modelData")
    public R<Void> modelDatalDelete(@RequestBody ModelDataDeleteBo modelDataDeleteBo){
        return  toAjax(isfdDataRetrievalService.modelDatalDelete(modelDataDeleteBo));
    }
    /**
     * 根据历史记录查询数据
     */
    @GetMapping("/search/history/{id}")
    public R<IsfdDataRetrievalListVo> dataRetrievalSearchByHistory(@PathVariable Long id){
        IsfdDataRetrievalListVo resultObject = isfdDataRetrievalService.dataRetrievalSearchByHistory(id);
        return R.ok(resultObject);
    }
}
